home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / mimetools.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  8KB  |  271 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Various tools used by MIME-reading or MIME-writing programs.'''
  5. import os
  6. import sys
  7. import tempfile
  8. from warnings import filterwarnings, catch_warnings
  9. catch_warnings().__enter__()
  10.  
  11. try:
  12.     import rfc822
  13. finally:
  14.     pass
  15.  
  16. from warnings import warnpy3k
  17. warnpy3k('in 3.x, mimetools has been removed in favor of the email package', stacklevel = 2)
  18. __all__ = [
  19.     'Message',
  20.     'choose_boundary',
  21.     'encode',
  22.     'decode',
  23.     'copyliteral',
  24.     'copybinary']
  25.  
  26. class Message(rfc822.Message):
  27.     '''A derived class of rfc822.Message that knows about MIME headers and
  28.     contains some hooks for decoding encoded and multipart messages.'''
  29.     
  30.     def __init__(self, fp, seekable = 1):
  31.         rfc822.Message.__init__(self, fp, seekable)
  32.         self.encodingheader = self.getheader('content-transfer-encoding')
  33.         self.typeheader = self.getheader('content-type')
  34.         self.parsetype()
  35.         self.parseplist()
  36.  
  37.     
  38.     def parsetype(self):
  39.         str = self.typeheader
  40.         if str is None:
  41.             str = 'text/plain'
  42.         
  43.         if ';' in str:
  44.             i = str.index(';')
  45.             self.plisttext = str[i:]
  46.             str = str[:i]
  47.         else:
  48.             self.plisttext = ''
  49.         fields = str.split('/')
  50.         for i in range(len(fields)):
  51.             fields[i] = fields[i].strip().lower()
  52.         
  53.         self.type = '/'.join(fields)
  54.         self.maintype = fields[0]
  55.         self.subtype = '/'.join(fields[1:])
  56.  
  57.     
  58.     def parseplist(self):
  59.         str = self.plisttext
  60.         self.plist = []
  61.         while str[:1] == ';':
  62.             str = str[1:]
  63.             if ';' in str:
  64.                 end = str.index(';')
  65.             else:
  66.                 end = len(str)
  67.             f = str[:end]
  68.             if '=' in f:
  69.                 i = f.index('=')
  70.                 f = f[:i].strip().lower() + '=' + f[i + 1:].strip()
  71.             
  72.             self.plist.append(f.strip())
  73.             str = str[end:]
  74.  
  75.     
  76.     def getplist(self):
  77.         return self.plist
  78.  
  79.     
  80.     def getparam(self, name):
  81.         name = name.lower() + '='
  82.         n = len(name)
  83.         for p in self.plist:
  84.             if p[:n] == name:
  85.                 return rfc822.unquote(p[n:])
  86.         
  87.  
  88.     
  89.     def getparamnames(self):
  90.         result = []
  91.         for p in self.plist:
  92.             i = p.find('=')
  93.             if i >= 0:
  94.                 result.append(p[:i].lower())
  95.                 continue
  96.         
  97.         return result
  98.  
  99.     
  100.     def getencoding(self):
  101.         if self.encodingheader is None:
  102.             return '7bit'
  103.         return self.encodingheader.lower()
  104.  
  105.     
  106.     def gettype(self):
  107.         return self.type
  108.  
  109.     
  110.     def getmaintype(self):
  111.         return self.maintype
  112.  
  113.     
  114.     def getsubtype(self):
  115.         return self.subtype
  116.  
  117.  
  118.  
  119. try:
  120.     import thread
  121. except ImportError:
  122.     catch_warnings().__exit__
  123.     catch_warnings().__exit__
  124.     catch_warnings()
  125.     import dummy_thread as thread
  126. except:
  127.     catch_warnings().__exit__
  128.  
  129. _counter_lock = thread.allocate_lock()
  130. del thread
  131. _counter = 0
  132.  
  133. def _get_next_counter():
  134.     global _counter
  135.     _counter_lock.acquire()
  136.     _counter += 1
  137.     result = _counter
  138.     _counter_lock.release()
  139.     return result
  140.  
  141. _prefix = None
  142.  
  143. def choose_boundary():
  144.     """Return a string usable as a multipart boundary.
  145.  
  146.     The string chosen is unique within a single program run, and
  147.     incorporates the user id (if available), process id (if available),
  148.     and current time.  So it's very unlikely the returned string appears
  149.     in message text, but there's no guarantee.
  150.  
  151.     The boundary contains dots so you have to quote it in the header."""
  152.     global _prefix
  153.     import time as time
  154.     if _prefix is None:
  155.         import socket as socket
  156.         
  157.         try:
  158.             hostid = socket.gethostbyname(socket.gethostname())
  159.         except socket.gaierror:
  160.             hostid = '127.0.0.1'
  161.  
  162.         
  163.         try:
  164.             uid = repr(os.getuid())
  165.         except AttributeError:
  166.             uid = '1'
  167.  
  168.         
  169.         try:
  170.             pid = repr(os.getpid())
  171.         except AttributeError:
  172.             pid = '1'
  173.  
  174.         _prefix = hostid + '.' + uid + '.' + pid
  175.     
  176.     return '%s.%.3f.%d' % (_prefix, time.time(), _get_next_counter())
  177.  
  178.  
  179. def decode(input, output, encoding):
  180.     '''Decode common content-transfer-encodings (base64, quopri, uuencode).'''
  181.     if encoding == 'base64':
  182.         import base64
  183.         return base64.decode(input, output)
  184.     if encoding == 'quoted-printable':
  185.         import quopri as quopri
  186.         return quopri.decode(input, output)
  187.     if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
  188.         import uu as uu
  189.         return uu.decode(input, output)
  190.     if encoding in ('7bit', '8bit'):
  191.         return output.write(input.read())
  192.     if encoding in decodetab:
  193.         pipethrough(input, decodetab[encoding], output)
  194.     else:
  195.         raise ValueError, 'unknown Content-Transfer-Encoding: %s' % encoding
  196.     return encoding in ('7bit', '8bit')
  197.  
  198.  
  199. def encode(input, output, encoding):
  200.     '''Encode common content-transfer-encodings (base64, quopri, uuencode).'''
  201.     if encoding == 'base64':
  202.         import base64
  203.         return base64.encode(input, output)
  204.     if encoding == 'quoted-printable':
  205.         import quopri
  206.         return quopri.encode(input, output, 0)
  207.     if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
  208.         import uu
  209.         return uu.encode(input, output)
  210.     if encoding in ('7bit', '8bit'):
  211.         return output.write(input.read())
  212.     if encoding in encodetab:
  213.         pipethrough(input, encodetab[encoding], output)
  214.     else:
  215.         raise ValueError, 'unknown Content-Transfer-Encoding: %s' % encoding
  216.     return encoding in ('7bit', '8bit')
  217.  
  218. uudecode_pipe = '(\nTEMP=/tmp/@uu.$$\nsed "s%^begin [0-7][0-7]* .*%begin 600 $TEMP%" | uudecode\ncat $TEMP\nrm $TEMP\n)'
  219. decodetab = {
  220.     'uuencode': uudecode_pipe,
  221.     'x-uuencode': uudecode_pipe,
  222.     'uue': uudecode_pipe,
  223.     'x-uue': uudecode_pipe,
  224.     'quoted-printable': 'mmencode -u -q',
  225.     'base64': 'mmencode -u -b' }
  226. encodetab = {
  227.     'x-uuencode': 'uuencode tempfile',
  228.     'uuencode': 'uuencode tempfile',
  229.     'x-uue': 'uuencode tempfile',
  230.     'uue': 'uuencode tempfile',
  231.     'quoted-printable': 'mmencode -q',
  232.     'base64': 'mmencode -b' }
  233.  
  234. def pipeto(input, command):
  235.     pipe = os.popen(command, 'w')
  236.     copyliteral(input, pipe)
  237.     pipe.close()
  238.  
  239.  
  240. def pipethrough(input, command, output):
  241.     (fd, tempname) = tempfile.mkstemp()
  242.     temp = os.fdopen(fd, 'w')
  243.     copyliteral(input, temp)
  244.     temp.close()
  245.     pipe = os.popen(command + ' <' + tempname, 'r')
  246.     copybinary(pipe, output)
  247.     pipe.close()
  248.     os.unlink(tempname)
  249.  
  250.  
  251. def copyliteral(input, output):
  252.     while None:
  253.         line = input.readline()
  254.         if not line:
  255.             break
  256.         
  257.         continue
  258.         return None
  259.  
  260.  
  261. def copybinary(input, output):
  262.     BUFSIZE = 8192
  263.     while None:
  264.         line = input.read(BUFSIZE)
  265.         if not line:
  266.             break
  267.         
  268.         continue
  269.         return None
  270.  
  271.